package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CompanyDao;
import com.itheima.domain.Company;
import com.itheima.service.CompanyService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;
import java.util.stream.Stream;

public class CompanyServiceImpl implements CompanyService {
    @Override
    public PageInfo<Company> findByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = null;
        PageInfo<Company> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CompanyDao companyDao = MybatisUtil.getMapper(sqlSession, CompanyDao.class);
            PageHelper.startPage(currentPage,pageSize);
            List<Company> list=companyDao.findAll();
            info = new PageInfo<>(list);
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }

    @Override
    public void save(Company company) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CompanyDao companyDao = MybatisUtil.getMapper(sqlSession, CompanyDao.class);
            String id = UUID.randomUUID().toString();
            company.setId(id);
            companyDao.save(company);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public Company findById(String id) {
        SqlSession sqlSession = null;
        Company company= null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CompanyDao companyDao = MybatisUtil.getMapper(sqlSession, CompanyDao.class);
            company = companyDao.findById(id);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return company;
    }

    @Override
    public void update(Company company) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CompanyDao companyDao = MybatisUtil.getMapper(sqlSession, CompanyDao.class);

            companyDao.update(company);
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CompanyDao companyDao = MybatisUtil.getMapper(sqlSession, CompanyDao.class);
            Stream.of(ids).forEach(id->companyDao.delete(id));
            MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Company> findAll() {
        SqlSession sqlSession = null;
        List<Company> info = null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            CompanyDao companyDao = MybatisUtil.getMapper(sqlSession, CompanyDao.class);

            info=companyDao.findAll();
        } finally {
            MybatisUtil.close(sqlSession);
        }

        return info;
    }
}
